package com.wake.mycomposebook.ui.home.myhome

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Palette
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SmallTopAppBar
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.wake.data.DemoDataProvider
import com.wake.data.model.HomeScreenItems
import com.wake.mycomposebook.ui.home.list.my.MyListActivity
import com.wake.mycomposebook.ui.home.modifier.ModifierActivity
import com.wake.mycomposebook.ui.sys.AppThemeState
import com.wake.mycomposebook.utils.showLog
import com.wake.theme.modifier.components.Material3Card

/**
 *项目主体
 * @param appThemeState
 * @param palletBottomSheetState
 * @param showMenu
 */
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun StudyHomeScreen(
    appThemeState: MutableState<AppThemeState>,
    palletBottomSheetState: ModalBottomSheetState,
) {
    val showMenu = remember {
        mutableStateOf(false)
    }
    Scaffold(
        topBar = {
            SmallTopAppBar(
                title = {
                    Text(text = "学成Compose")
                },
                actions = {
                    IconButton(onClick = {
                        appThemeState.value = appThemeState.value.copy(darkTheme = !appThemeState.value.darkTheme)
                    }) {
                        Icon(painter = painterResource(id = com.wake.theme.R.drawable.ic_sleep), contentDescription = null)
                    }
                    BtColorPallet(showMenu)
                }
            )
        },
        content = {
            AfHomeContent(showMenu, appThemeState)
        }
    )
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun BtColorPallet(showMenu: MutableState<Boolean>) {
    IconButton(onClick = {
        showMenu.value = !showMenu.value
    }) {
        Icon(imageVector = Icons.Default.Palette, contentDescription = null)
    }
}


/**
 * 显示主要内容
 *  去掉了无障碍和宽屏显示
 */
@Composable
fun AfHomeContent(showMenu: MutableState<Boolean>, appThemeState: MutableState<AppThemeState>) {
    val context = LocalContext.current
    Box {
        val itemList = DemoDataProvider.homeScreenListItems
        LazyColumn {
            items(itemList) {
                AfHomeListItem(it) {
                    showMenu.value = false
                    when (it.name) {
                        "Vertical ListView",
                        "Horizontal ListView",
                        "Grid ListView" -> {
                            MyListActivity.newIntent(context, appThemeState.value.darkTheme, it.name)
                        }
                        else -> {
                            ModifierActivity.newIntent(context = context, it.name,appThemeState.value.darkTheme)
                        }
                    }

                }
            }
        }
        if (showMenu.value) {
            MyColorPallet(modifier = Modifier.align(Alignment.TopEnd)) {
                appThemeState.value = appThemeState.value.copy(pallet = it)
                showMenu.value = false
            }
        }
    }
}


@Composable
fun AfHomeListItem(item: HomeScreenItems, callBack: () -> Unit) {
    Material3Card(
        modifier = Modifier
            .clickable { callBack.invoke() }
            .fillMaxWidth()
            .padding(8.dp),
        backgroundColor = MaterialTheme.colorScheme.primary,
        shape = RoundedCornerShape(8.dp),
        elevation = 4.dp,
        contentColor = MaterialTheme.colorScheme.onPrimary
    ) {
        Text(
            text = item.name,
            modifier = Modifier.padding(8.dp),
            style = MaterialTheme.typography.titleMedium
        )
    }
//    Button(
//        onClick = {
//            afItemClick(item = item)
//            callBack.invoke()
//        },
//        modifier = Modifier
//            .padding(10.dp)
//            .fillMaxWidth(),
//        shape = RoundedCornerShape(15.dp),
//        ) {
//        Text(
//            text = item.name,
//            color = MaterialTheme.colorScheme.onPrimary,
//            style = MaterialTheme.typography.titleMedium,
//            modifier = Modifier
//                .padding(6.dp)
//        )
//    }
}

fun afItemClick(item: HomeScreenItems) {
    showLog(item.name)
}


@OptIn(ExperimentalMaterialApi::class)
@Preview
@Composable
fun previewScreen() {
    val appThemeState = remember {
        mutableStateOf(AppThemeState(false))
    }
    val palletBottomSheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)


    StudyHomeScreen(appThemeState = appThemeState, palletBottomSheetState = palletBottomSheetState)

}
